'use server';
import Title from 'antd/es/typography/Title';
import './index.css';
import QuestionTable from '@/components/QuestionTable';
import { searchQuestionVoUsingPost } from '@/api/QuestionApi';

/**
 * 题目列表页面
 * @constructor
 */
export default async function QuestionPage({ searchParams }) {
  const { q: searchText } = searchParams;
  let questionList = [];
  let total = 0;

  try {
    const res = await searchQuestionVoUsingPost({
      title: searchText,
      pageSize: 20,
      sortField: 'createTime',
      sortOrder: 'descend',
    });
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-expect-error
    questionList = res.data.records ?? [];
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-expect-error
    total = res.data.total ?? 0;
  } catch (e) {
    console.error('Failed to fetch bank list:', e);
  }

  return (
    <div id="question-page" className="max-width-content">
      <Title level={3}>全部题库</Title>
      <QuestionTable
        defaultQuestionList={questionList}
        defaultTotal={total}
        defaultSearchParams={{
          title: searchText,
        }}
      />
    </div>
  );
}
